<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Session.class.php";
require_once dirname(__FILE__)."/Config.class.php";
require_once dirname(__FILE__)."/Cookie.class.php";
require_once dirname(__FILE__)."/SubscriptionManager.class.php";

class SessionRoster extends Session
{
	static private $instance;
	
	private $cookie;
	private $subscription_manager = null;
	
	protected function SessionRoster()
	{
		parent::__construct();
		
		$this->cookie = Cookie::GetInstance();
		
		if ( $this->GetVar('current_locale') == null )
		{
			$config = Config::GetInstance();
			
			$this->SaveVar('current_locale', $config->GetLocalization());
		}
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new SessionRoster();
			self::$instance->RenewConnection();
		}
		
		return self::$instance;
	}
	
	private function RenewConnection()
	{
		if ($this->IsConnected() == false and $this->cookie->GetCookie('side_meyer') != null)
		{
			$this->subscription_manager = SubscriptionManager::GetInstance();
			
			if ( $this->subscription_manager->IsSidValid($this->cookie->GetCookie('side_meyer')) == true )
			{
				$this->ConnectWithLocale( $this->subscription_manager->GetSubscription($this->subscription_manager->GetAccountIdBySid($this->cookie->GetCookie('side_meyer'))), $this->subscription_manager->GetAccountLocaleBySid($this->cookie->GetCookie('side_meyer')) );
			}
			else
			{
				$this->cookie->DelCookie('side_meyer');
			}
		}
	}
	
	function GetLocalization()
	{
		return $this->GetVar('current_locale');
	}
	
	function SetLocalization($l)
	{
		$this->SaveVar('current_locale', $l);
	}
	
	function SetDestination($dest)
	{
		if ($dest != "change_language.php")
		{
			$this->SaveVar('destination', $dest);
		}
	}
	
	function GetDestination()
	{
		$dest = "index.php";
		
		if ($this->GetVar('destination') != null)
		{
			$dest = $this->GetVar('destination');
			$this->DelVar('destination');
		}
		
		return $dest;
	}
	
	function ConnectWithLocale($subscription, $localization)
	{
		if ($this->subscription_manager == null)
		{
			$this->subscription_manager = SubscriptionManager::GetInstance();
		}
		
		$this->subscription_manager->SetSid( $subscription->GetId(), $this->GetSessionId() );
		
		$this->SaveVar('is_connected', $subscription->GetId());
		$this->SaveVar('account_name', $subscription->GetName());
		$this->SetLocalization($localization);
		
		$this->cookie->SaveCookie('side_meyer', $this->GetSessionId());
	}
	
	function ConnectWithoutCookie($subscription)
	{
		if ($this->subscription_manager == null)
		{
			$this->subscription_manager = SubscriptionManager::GetInstance();
		}
		
		$this->subscription_manager->SetSid( $subscription->GetId(), $this->GetSessionId() );
		
		$this->SaveVar('is_connected', $subscription->GetId());
		$this->SaveVar('account_name', $subscription->GetName());
	}
	
	function ConnectAdmin()
	{
		$this->SaveVar('admin_connected', "yes");
	}
	
	function IsAdminConnected()
	{
		if ( $this->GetVar('admin_connected') != null )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function GetAccountId()
	{
		return $this->GetVar('is_connected');
	}
	
	function GetAccountName()
	{
		return $this->GetVar('account_name');
	}
	
	function IsConnected()
	{
		if ( $this->GetVar('is_connected') != null )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function Disconnect()
	{
		$config = Config::GetInstance();
		
		session_destroy();
		session_regenerate_id();
		
		$this->session_id = session_id(); // Don't use $this->GetSessionId()
		$this->DelVar('is_connected');
		$this->SetLocalization( $config->GetLocalization() );
		
		$this->cookie->DelCookie('side_meyer');
	}
}

?>